package com.zl.learning.thread;

import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class TestThreadPool2 {
    private static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);

    public static void main(String[] args) {
        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                executor.submit(new Work());
                sleep(6);
            }
        }).start();

//        while (executor.getActiveCount()<executor.getCorePoolSize()){
//            System.out.println("Thread is idle:"+executor.getActiveCount()+"-------"+executor.getCorePoolSize());
//        }
//        executor.submit(new Work());
        while (true){
            sleep(2);
            System.out.format("getTaskCount：%d，getActiveCount：%d，getCompletedTaskCount：%d，getCorePoolSize：%d，getPoolSize：%d，getLargestPoolSize：%d，getMaximumPoolSize：%d"
                    ,executor.getTaskCount()
                    , executor.getActiveCount()
                    , executor.getCompletedTaskCount()
                    , executor.getCorePoolSize()
                    , executor.getPoolSize()
                    , executor.getLargestPoolSize()
                    , executor.getMaximumPoolSize()
            );
            System.out.println("-----------------");
        }

    }

    private static void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static long getRandom() {
        return new Random().nextInt(1000);
    }

    private static class Work implements Runnable {

        @Override
        public void run() {
            sleep(getRandom());
        }
    }
}
